package mi.payment.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Locale;

import mi.payment.R;
import mi.payment.db.dao.PayDetail;
import mi.payment.tuple.Tuple;
import mi.payment.tuple.TupleUtil;
import mi.payment.utils.MoneyUtils;
import mi.payment.utils.OrderJudgmentUtil;
import mi.payment.utils.SystemDateTime;


/**
 * 交易查询Adapter
 */
public class TransactionQueryAdapter extends BaseListAdapter<PayDetail> {

    public  TransactionQueryAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_trans_query, parent, false);
            holder.resultIcon = (ImageView) convertView.findViewById(R.id.ic_result);
            holder.resultDesc = (TextView) convertView.findViewById(R.id.tv_result_desc);
            holder.transAmount = (TextView) convertView.findViewById(R.id.tv_trans_amount);
            holder.transModelIcon = (ImageView) convertView.findViewById(R.id.ic_trans_model);
            holder.voucherNo = (TextView) convertView.findViewById(R.id.tv_voucher_num);
            holder.transTime = (TextView) convertView.findViewById(R.id.tv_trans_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        PayDetail item = getItem(position);
        Tuple<Drawable, String> tuple = getTransResult(item);
        holder.resultIcon.setImageDrawable(tuple.a);
        holder.resultDesc.setText(tuple.b);
        holder.transAmount.setText(getAmount(item));
        holder.transModelIcon.setImageDrawable(getTransactionIcon(item));
        holder.voucherNo.setText(getVoucherNo(item));
        holder.transTime.setText(getTradeTime(item));

        return convertView;
    }

    /**
     * 获取交易结果的图标和说明文字
     */
    private Tuple<Drawable, String> getTransResult(PayDetail item) {
        int resIconId = -1;
        int resStrId = -1;
        if (item.transPlatform == 0) {
            // 银行卡消费
            if (TextUtils.equals("00", item.tradeAnswerCode)) {
                resIconId = R.drawable.ic_success;
                resStrId = R.string.trans_succeed;
                if (item.isCanceled) {
                    resStrId = R.string.trans_canceled;
                }
            } else {
                resIconId = R.drawable.ic_failed;
                resStrId = R.string.trans_failed;
            }
        } else if (item.transPlatform > 0) {
            // 扫码类交易
            if (OrderJudgmentUtil.isQRUnknownOrder(item)) {
                resIconId = R.drawable.ic_unknown;
                resStrId = R.string.trans_unknown;
            } else if (TextUtils.equals("00", item.tradeAnswerCode) && item.QRCodeTransState == 1) {
                resIconId = R.drawable.ic_success;
                resStrId = R.string.trans_succeed;
                if (item.isCanceled) {
                    resStrId = R.string.trans_canceled;
                }
            } else {
                resIconId = R.drawable.ic_failed;
                resStrId = R.string.trans_failed;
            }
        }
        Drawable drawable = null;
        String desc = null;
        if (resIconId != -1) {
            drawable = getContext().getResources().getDrawable(resIconId);
        }
        if (resStrId != -1) {
            desc = getContext().getString(resStrId);
        }
        return TupleUtil.tuple(drawable, desc);
    }

    private String getAmount(PayDetail item) {
        String amount = MoneyUtils.longCent2DoubleMoneyStr(item.amount);
        return getContext().getString(R.string.amount, amount);
    }

    private Drawable getTransactionIcon(PayDetail item) {
        int resId = -1;
        switch (item.transPlatform) {
            case 0: // 银行卡
                resId = R.drawable.item_bank_card;
                break;
            case 1: // 支付宝
                resId = R.drawable.item_ali_code;
                break;
            case 2: // 微信
                resId = R.drawable.item_wx_code;
                break;
            case 3: // 口碑
                resId = R.drawable.item_koubei_er_code;
                break;
            case 4: // 银联二维码
                resId = R.drawable.item_union_code;
                break;
            case 5: //小米钱包
                resId = R.drawable.item_mi_code;
                break;
            default:
                resId = R.drawable.item_ali_code;
                break;
        }
        return getContext().getResources().getDrawable(resId);
    }

    private String getVoucherNo(PayDetail item) {
        String proof = getContext().getString(R.string.proof);
        return String.format(Locale.getDefault(), "%s： %s", proof, item.voucherNo);
    }

    private String getTradeTime(PayDetail payDetail) {
        return SystemDateTime.getTimeByPayDetails(payDetail);
    }

    private static class ViewHolder {
        private ImageView resultIcon;   // 交易结果Icon
        private TextView resultDesc;    // 交易结果文字说明
        private TextView transAmount;   // 交易金额
        private ImageView transModelIcon; // 交易方式Icon
        private TextView voucherNo;     // 凭证号
        private TextView transTime;     // 交易时间
    }

}
